package com.ruoyi.web;

import cn.hutool.poi.excel.ExcelFileUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.poi.ExcelUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Test {
    public static void main(String[] args) throws Exception {
        ExcelUtil<TaskVo> util = new ExcelUtil<TaskVo>(TaskVo.class);
        File file = new File("D:\\work\\platform\\agriculture\\ruoyi-admin\\src\\main\\java\\com\\ruoyi\\web\\核查经纬度.xls");
        List<TaskVo> userList = util.importExcel(new FileInputStream(file));
        Map<String, List<TaskVo>> collect =
                userList.stream().collect(Collectors.groupingBy(TaskVo::getTaskName));
        ExcelUtil<TaskResultVo> utils = new ExcelUtil<TaskResultVo>(TaskResultVo.class);
        List<TaskResultVo> resultVos = new ArrayList<>();
//        ExcelWriter writer = cn.hutool.poi.excel.ExcelUtil.getWriter(true);
//        // 设置别名
//        writer.addHeaderAlias("任务名称", "taskName");
//        writer.addHeaderAlias("位置", "location");
        File fileOut = new File("d:","任务经纬度.csv");
        collect.forEach((key,vals)->{
            TaskResultVo resultVo = new TaskResultVo();
            List<String> collect1 = vals.stream().flatMap(item -> {
                String location = item.getLocation();
                return JSONArray.parseArray(location, String.class).stream();
            }).collect(Collectors.toList());
            resultVo.setLocation(JSON.toJSONString(collect1));
            resultVo.setTaskName(key);
            resultVos.add(resultVo);
        });


//        ExcelFileUtil
//        utils.exportExcel(resultVos, "用户数据");

//    // 第一种，Map
//        List<Map<String, Object>> mapList = new ArrayList<>();
//        Map map = new HashMap();
//        map.put("name", "Tom");
//        map.put("age", 25);
//        mapList.add(map);
//        writer.write(mapList, true);
//
//        // 第二种，实体类
//        List<User> userList = new ArrayList<>();
//        userList.add(new User("Tom", 25));
//        writer.write(resultVos, true);
//        writer.flush(new FileOutputStream("D:/任务位置.xls"));
//        writer.close();
    }
}
